﻿namespace EFS.RC.CC.CH.Tables
{
	public static class Surveys
	{
		/// Returns an object from a data row
		public static EFS.RC.CC.CH.Entities.Surveys.SurveyInfo FromTableRow(System.Data.DataRow row)
		{
			// check
			EFS.Common.Verify.Reference(row, "row");

			// create new
			EFS.RC.CC.CH.Entities.Surveys.SurveyInfo dataObj = new EFS.RC.CC.CH.Entities.Surveys.SurveyInfo();

			// ID
			dataObj.ID = EFS.DB.DataRow.Int32(row, "ID");

			// SURVEY_QUESTION_ID
			dataObj.Name = EFS.DB.DataRow.String(row, "NAME");

			// template
			dataObj.IsTemplate = (EFS.DB.DataRow.Int32(row, "TEMPLATE") == 1) ? "Yes" : "No";

			// TYPE
			System.Int32? nType = EFS.DB.DataRow.Int32(row, "TYPE");
			dataObj.Type = null;
			if (nType.HasValue)
			{
				dataObj.Type = (EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eType)nType.Value;
			}

			// REVIEW_TYPE
			System.Int32? nReviewType = EFS.DB.DataRow.Int32(row, "REVIEW_TYPE");
			dataObj.ReviewType = null;
			if (nReviewType.HasValue)
			{
				dataObj.ReviewType = (EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eReviewType)nReviewType.Value;
			}

			// ok
			return dataObj;
		}

		/// GetAll
		public static EFS.RC.CC.CH.Entities.Surveys.SurveyInfo[] GetAll(System.Int32? template, EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eType? type, EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eReviewType? reviewType)
		{
			// handle connection
			using (EFS.DB.Connection connection = EFS.RC.CC.CH.QRDConnection.New)
			{
				// get data
				return connection.DoExecuteTableReaderQuery<EFS.RC.CC.CH.Entities.Surveys.SurveyInfo>(
					"[dbo].[Surveys_GetAll]",
					new EFS.DB.Parameters.Array(
							new System.String[] { "template", "type", "reviewType" },
							new System.Object[] { template, (System.Int32?)type, (System.Int32?)reviewType }),
					FromTableRow);
			}
		}

		/// GetAll
		public static EFS.RC.CC.CH.Entities.Surveys.SurveyInfo[] GetAvailableConversationSurveysForUser(
			System.String user,
			System.Guid conversationGuid,
			EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eReviewType? reviewType)
		{
			// handle connection
			using (EFS.DB.Connection connection = EFS.RC.CC.CH.QRDConnection.New)
			{
				// get data
				return connection.DoExecuteTableReaderQuery<EFS.RC.CC.CH.Entities.Surveys.SurveyInfo>(
					"[dbo].[SurveySessions_GetAvailableConversationSurveysForUser]",
					new EFS.DB.Parameters.Array(
						new System.String[] { "survey_by", "conversation_guid", "reviewType" },
						new System.Object[] { user, conversationGuid, (System.Int32)reviewType },
						new System.Boolean[] { false, false, false }),
					FromTableRow);
			}
		}

		/// GetAll
		public static EFS.RC.CC.CH.Entities.Surveys.SurveyInfo[] GetAvailableDocumentsSurveysForUser(
			System.String user,
			System.String documentId,
			EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eReviewType? reviewType)
		{
			// handle connection
			using (EFS.DB.Connection connection = EFS.RC.CC.CH.QRDConnection.New)
			{
				// get data
				return connection.DoExecuteTableReaderQuery<EFS.RC.CC.CH.Entities.Surveys.SurveyInfo>(
					"[dbo].[SurveySessions_GetAvailableDocumentsSurveysForUser]",
					new EFS.DB.Parameters.Array(
						new System.String[] { "survey_by", "document_id", "reviewType" },
						new System.Object[] { user, documentId, (System.Int32)reviewType },
						new System.Boolean[] { false, false, false }),
					FromTableRow);
			}
		}

		/// GetNumberOfQuestions
		public static System.Int32 GetNumberOfQuestions(System.Int32 surveyID)
		{
			{
				// make params
				EFS.DB.Parameters.Array parametersArray =
					new EFS.DB.Parameters.Array(
						new System.String[] { "survey_id" },
						new System.Object[] { surveyID },
						new System.Boolean[] { false });

				// handle connection
				using (EFS.DB.Connection connection = EFS.RC.CC.CH.QRDConnection.New)
				{
					// exec create
					using (System.Data.DataSet resultDataSet = connection.DoExecuteQuery("[dbo].[Survey_GetCountNumberOfQuestions]", parametersArray))
					{
						if (resultDataSet.Tables.Count > 0)
						{
							// get tables
							using (System.Data.DataTable table = resultDataSet.Tables[0])
							{
								if (table.Rows.Count > 0)
								{
									return EFS.DB.DataRow.Int32(table.Rows[0], "CNT").Value;
								}
							}
						}
					}

					// none
					return 0;
				}
			}
		}

		/// GetByID
		public static EFS.RC.CC.CH.Entities.Surveys.SurveyInfo GetByID(System.Int32 id)
		{
			// handle connection
			using (EFS.DB.Connection connection = EFS.RC.CC.CH.QRDConnection.New)
			{
				// get data
				return connection.DoExecuteRowReaderQuery<EFS.RC.CC.CH.Entities.Surveys.SurveyInfo>(
					"[dbo].[Surveys_GetByID]",
					new EFS.DB.Parameters.Array("id", id),
					FromTableRow);
			}
		}

		/// Create
		public static System.Int32? Create(
			System.String name,
			EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eType type,
			System.Int32 isTemplate, 
			System.Int32 templateID,
			EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eReviewType reviewType)
		{
			// make params
			EFS.DB.Parameters.Array parametersArray = new EFS.DB.Parameters.Array(
				new System.String[] { "name", "type", "isTemplate", "templateID", "reviewType", "id" },
				new System.Object[] { name, (System.Int32)type, isTemplate, templateID, (System.Int32)reviewType, default(System.Int32) },
				new System.Boolean[] { false, false, false, false, false, true });

			// handle connection
			using (EFS.DB.Connection connection = EFS.RC.CC.CH.QRDConnection.New)
			{

				// exec create
				connection.DoExecuteNonQuery("[dbo].[Surveys_Create]", parametersArray);
			}

			// get id value
			return parametersArray.GetOutPrameterValue<System.Int32?>("id"); 
		}

		/// Update
		public static void Update(
			System.Int32 id,
			EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eType type,
			System.String name,
			EFS.RC.CC.CH.Entities.Surveys.SurveyInfo.eReviewType reviewType)
		{			
			// handle connection
			using (EFS.DB.Connection connection = EFS.RC.CC.CH.QRDConnection.New)
			{
				connection.DoExecuteNonQuery(
					"[dbo].[Surveys_Update]",
					new EFS.DB.Parameters.Array(
						new System.String[] { "id", "type", "name", "reviewType" },
						new System.Object[] { id, (System.Int32)type, name, (System.Int32)reviewType }));
			}
		}

		/// Delete
		public static void Delete(System.Int32 id)
		{
			// handle connection
			using (EFS.DB.Connection connection = EFS.RC.CC.CH.QRDConnection.New)
			{
				connection.DoExecuteNonQuery("[dbo].[Surveys_Delete]", new EFS.DB.Parameters.Array("id", id));
			}
		}
	}

}